/*
 * Created by Icodeon Ltd.
 * Date: 09/08/2008
 * 
 * Copyright 2008 All Rights Reserved
 * Icodeon Ltd 
 * St Johns Innovation Centre
 * Cowley Road
 * Cambridge 
 * CB4 0WS United Kingdom
 * 
 * Registered in England and Wales No: 5068195
 * Use is subject to license terms.
 */

using System;
using System.Xml;
using System.Collections;

namespace Icodeon.Util
{
    public class XmlUtils
    {
        public XmlUtils() { }

        public void AddNewAttribute(XmlElement element, string localAttributeName, string value)
        {
            XmlDocument domDoc = element.OwnerDocument;
            XmlAttribute attr = domDoc.CreateAttribute(localAttributeName);
            attr.Value = value;
            XmlAttributeCollection attrColl = element.Attributes;
            attrColl.Append(attr);
        }

        public string GetAttributeValue(XmlElement element, string localAttributeName)
        {
            XmlAttributeCollection attrColl = element.Attributes;
            for (int i = 0; i < attrColl.Count; i++)
            {
               if(attrColl[i].LocalName.Equals(localAttributeName)){
                   return attrColl[i].InnerText;
               }
            }

            return null;
        }

        public bool SetAttributeValue(XmlElement element, string localAttributeName, string value)
        {
            XmlAttributeCollection attrColl = element.Attributes;
            for (int i = 0; i < attrColl.Count; i++)
            {
                if (attrColl[i].LocalName.Equals(localAttributeName))
                {
                    attrColl[i].InnerText = value;
                    return true;
                }
            }
            return false;
        }

        public XmlElement GetNamedChildElement(XmlElement element, string localElementName)
        {
            XmlNodeList nodeList = element.ChildNodes;
            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode child = nodeList[i];
                if (child.NodeType == XmlNodeType.Element)
                {
                    if (child.LocalName.Equals(localElementName))
                    {
                        return (XmlElement)child;
                    }
                }
            }
            return null;
        }

        public XmlElement[] GetNamedChildElements(XmlElement element, string localElementName)
        {
            ArrayList arrayList = new ArrayList();
            XmlNodeList nodeList = element.ChildNodes;
            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode child = nodeList[i];
                if (child.NodeType == XmlNodeType.Element)
                {
                    if (child.LocalName.Equals(localElementName))
                    {
                        arrayList.Add((XmlElement)child);
                    }
                }
            }

            return (XmlElement[])arrayList.ToArray(typeof(XmlElement));
        }
    }
}
